import numpy as np
import cv2 as cv

'''
函数描述：重叠两张图片，以确定边缘的准确性。
JZhou@20211122
'''

def scan_pic(edge_img):
    # 提取边缘图中的点集
    points = []
    img_info = edge_img.shape
    rows = img_info[0]
    cols = img_info[1]
    for i in range(rows):
        for j in range(cols):
            k = edge_img[i, j] # 二值图的像素点
            if not k == 0:
                points.append([j, i])
    return points


def write_res2img(points, img):
    # 把边缘图和原图重合
    img_array = np.array(img)
    img2_array = img_array
    for p in points:
        img2_array[p[0],p[1]]=np.array([0, 255, 255])
    return img2_array


def write2PicAlpha(points, img):
    # 把边缘图和原图叠加，通过调整透明度叠加。
    mask = img.copy()
    for point in points:
        cv.circle(mask, (point[0], point[1]), 1, (0, 255, 0))
    alpha = 0.25
    result = cv.addWeighted(mask, alpha, img, 1 - alpha, 0)
    return result


def superPosition(edge_img, init_img):
    # 提取边缘图中的点集
    points = []
    img_info = edge_img.shape
    rows = img_info[0]
    cols = img_info[1]
    for i in range(rows):
        for j in range(cols):
            k = edge_img[i, j] # 二值图的像素点
            if not k == 0:
                points.append([i, j])
    return write_res2img(points, init_img)


def superPositionAlpha(edge_img, init_img):
    points = scan_pic(edge_img)
    return write2PicAlpha(points, init_img)

